home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / varia / egebook.lha / ege.book / 3 / binding.C next >
C/C++ Source or Header  |  1992-06-05  |  1KB  |  81 lines

  1. #include <stdio.h>
  2. #include "bool.h"
  3.  
  4. // Class definition for Person
  5.  
  6. class Person {
  7.   char* name;
  8. public:
  9.   Person();                // constructor
  10.   Person(char *n);         //    bodies declared elsewhere
  11.   void print();            // member function
  12. };
  13.  
  14. // Body for class Person
  15.  
  16.  Person::Person(){         // constructor
  17.    name = "";
  18.  }
  19.  
  20.  Person::Person(char *n){  // constructor
  21.    name = n;
  22.  };
  23.  
  24.  void Person::print(){     // member functions
  25.    printf("my name is: %s \n", name);
  26.  };
  27.  
  28. // Class definition for Teacher
  29.                                 // Teacher is derived 
  30. class Teacher: public Person {  //        from Person
  31.   int courses;
  32.   static int MaxCourses;
  33. public:
  34.   Teacher(char *name):Person(name){
  35.     courses = 0;
  36.   }
  37.   bool check();
  38.   void addCourse();
  39. };
  40.  
  41. // Body for class Teacher
  42.  
  43. int Teacher::MaxCourses = 2;
  44.  
  45. bool Teacher::check(){
  46.   return (bool) (courses < MaxCourses);
  47. }
  48.  
  49. void Teacher::addCourse() {
  50.   if (check()) 
  51.     courses++; 
  52. }
  53.  
  54. // Student class declaration
  55.  
  56. class Student: public Person{
  57.  public:
  58.   Student(char *name):Person(name){};
  59.   void print();
  60. };
  61.  
  62. // Body for class Student
  63.  
  64. void Student::print(){
  65.   printf("Student: ");
  66.   Person::print();
  67. }
  68.  
  69. main() {
  70.   Teacher t("John Prof");
  71.   Student s("Hugo Meier");
  72.   Person *array[2];
  73.   array[0] = &t;
  74.   array[1] = &s;   // array holding pointers to Person,
  75.                    // initialized with pointers to t and s
  76.   t.print();
  77.   s.print();
  78.   array[0]->print();
  79.   array[1]->print();
  80. }
  81.